package com.itboye.sunsun.adapter;

import java.util.List;

import com.itboye.sunsun.R;
import com.itboye.sunsun.adapter.AbstractOrderAdapter.ReceiveHolder;
import com.itboye.sunsun.beans.CustomBean;
import com.itboye.sunsun.beans.OrderBean.InnerDetailBean;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.DebugToast;
import com.itboye.sunsun.utils.DoubleParse;
import com.itboye.sunsun.utils.XImageLoader;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListViewAdapter extends BaseAdapter {

	private Context context;
	private List<InnerDetailBean> list;

	public CustomListViewAdapter(Context context, List<InnerDetailBean> dataList) {
		this.context = context;
		this.list = dataList;
	}
	
	
	

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return list.get(position).getViewType();
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 4;
	}

	final class BottomViewHolder {

		TextView price;
	}

	final class TopViewHolder {

		TextView state;
	}

	final class GoodsViewHolder {
		ImageView goods;

		TextView name;
		TextView count;
		TextView guige;
		TextView price;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		int type = getItemViewType(position);

		if (type == 0) {
			return createGoodsView(position, convertView, parent);
		}
		if (type == 1) {
			return createTopView(position, convertView, parent);
		}
		if (type == 2) {
			return crateBottomView(position, convertView, parent);
		}
		return null;

	}

	private View createTopView(int position, View convertView, ViewGroup parent) {
		TopViewHolder holder;
		
		if(convertView==null)
		{
			holder=new TopViewHolder();
			convertView=LayoutInflater.from(context).inflate(R.layout.custom_item_top, parent,false);
			holder.state=(TextView) convertView.findViewById(R.id.state);
			convertView.setTag(holder);
		}else
		{
			holder=(TopViewHolder) convertView.getTag();
			DebugLog.v("liststate","wo"+ list.get(position).getState());
			//holder.state.setText(""+list.get(position).getState());
		}
		holder.state.setText(""+list.get(position).getState());
		
		return convertView;
	}

	// 货物详情Item
	private View createGoodsView(int position, View convertView, ViewGroup parent) {
		GoodsViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.item_order_name_picture_count, parent, false);
			holder = new GoodsViewHolder();
			// holdSuccess.text=(TextView) convertView.findViewById(R.id.text);
			convertView.setTag(holder);

			holder.goods = (ImageView) convertView.findViewById(R.id.goods);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.count = (TextView) convertView.findViewById(R.id.count);
			holder.guige = (TextView) convertView.findViewById(R.id.guige);
			holder.price = (TextView) convertView.findViewById(R.id.price);
		} else {
			holder = (GoodsViewHolder) convertView.getTag();

		}
		holder.name.setText(list.get(position).getName());
		holder.guige.setText(list.get(position).getSkuDesc());
		holder.price.setText(list.get(position).getPrice());
		XImageLoader.load(list.get(position).getImgUrl(), holder.goods);
		holder.count.setText("x" + list.get(position).getCount());
		return convertView;
	}

	// 底部Item
	private View crateBottomView(int position, View convertView, ViewGroup parent) {
		BottomViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.custom_item_back, parent, false);
			holder = new BottomViewHolder();
			holder.price=(TextView) convertView.findViewById(R.id.price);
			convertView.setTag(holder);

		} else {
			holder = (BottomViewHolder) convertView.getTag();
		}
		//DebugLog.v("price", list.get(position).getAllprice());
		holder.price.setText("交易金额:"+String.format("%.2f", DoubleParse.parse(list.get(position).getAllprice(),0)));
		return convertView;
	}

	
	

}